package routlist

import (
	"encoding/binary"
	"net"
	"sync"

	"gitee.com/yutiandou/wdRPC/common/routing"

	"gitee.com/yutiandou/wdtools/wdlog"
)

type RoutList struct {
	list       sync.Map
	lpAddress  string
	servername string
	nowServer  net.Conn
}

func GetRoutList(LPAddress string) (r *RoutList) {
	r = &RoutList{
		list: sync.Map{},
	}

	return
}

//----------------------------------------

func GetServerList(address, servername string) (list []routing.Node) {
	list = make([]routing.Node, 0)
	conn, err := net.Dial("tcp", address)
	if wdlog.Error(err) {
		return
	}
	datahead := make([]byte, 8)
	databody := []byte(servername)
	binary.LittleEndian.PutUint32(datahead[:4], 2)
	binary.LittleEndian.PutUint32(datahead[4:], uint32(len(databody)))
	datahead = append(datahead, databody...)
	_, err = conn.Write(datahead)
	if wdlog.Error(err) {
		return
	}
	return
}
